﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Web.Configuration;

/// <summary>
/// Bank Web Service for Project Monopoly
/// </summary>
[WebService(Namespace = "http://monopol.mk/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Banka : System.Web.Services.WebService {

    public Banka () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public bool insertNewPlayer(string username, int balance, string password)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["BankaDB"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);

            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("INSERT INTO Player VALUES (@username, @Balance, @Password)", conn);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@Balance", balance);
                cmd.Parameters.AddWithValue("@Password", password);
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }

    [WebMethod]
    public bool depositMoney (string username, int amount)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["BankaDB"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);
           
            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Balance=Balance+@Amount WHERE Username = @username", conn);
                cmd.Parameters.AddWithValue("@Amount", amount);
                cmd.Parameters.AddWithValue("@username", username);              
                if (cmd.ExecuteNonQuery() == 0) throw new Exception("Wrong ID or ID with that value does not exist...");
            }
        }
        catch (Exception err)
        {
             return false;
        }
        return true;
    }
    [WebMethod]
    public bool withdrawMoney (string username, int amount)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["BankaDB"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);

            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Balance=Balance-@Amount WHERE Username = @username", conn);
                cmd.Parameters.AddWithValue("@Amount", amount);
                cmd.Parameters.AddWithValue("@username", username);
                if (cmd.ExecuteNonQuery() == 0) throw new Exception("Wrong ID or ID with that value does not exist...");
            }
        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }
    [WebMethod]
    public int showBalance(string username)
    {
            string connectionString = WebConfigurationManager.ConnectionStrings["BankaDB"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);
            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("SELECT Balance FROM Player WHERE Username=@username", conn);
                cmd.Parameters.AddWithValue("@username", username);
                OleDbDataReader rd = cmd.ExecuteReader();

                //If there are no rows returned Exception is thrown
                rd.Read();

                return Convert.ToInt32(rd["Balance"].ToString());
                
            }
            //This code is unreachable
            return -1;
    }
    
}
